function setLeftMenu(url,itemid){
	var c=$(parent.document).children()[0];
	var frameset=$(c).children()[1];
	var menuFrame=$(frameset).children()[0];

	$(menuFrame).load(function(){
		setLeftMenuItem(itemid);
	});

	menuFrame.setAttribute("src",url);
}

function setLeftMenuItem(itemId){
	var c=$(parent.document).children()[0];
	var frameset=$(c).children()[1];
	var menuFrame=$(frameset).children()[0];

	var d=menuFrame.contentWindow.document;
	$(d).find("a").each(function(i){
		if($(this).attr("id")==itemId){
			$(this).addClass("menuItem");
		}else{
			$(this).removeClass("menuItem");
		}
	});
}


function setLeftMenuStyle(){
	$('.menu').each(function(i){
		$(this).bind('click',function(){
			$('.menuItem').each(function(i){
				$(this).removeClass("menuItem");
			});
			$(this).addClass("menuItem");
		});
	});
}

$(document).ready(function(){
	rowStyleClass();
	setLeftMenuStyle();
	addHiddenSubmitFrame();
	$.ajaxSetup({cache:false});
});

function addHiddenSubmitFrame(){
	if(!document.getElementById("tempFrame")){
		$('body').append($("<div style='display: none;'><iframe id='tempFrame' name='tempFrame' src=''></iframe></div>"));
	}
}

function rowStyleClass(){
	var i=0;
	$('.rowClass').each(function(i){
		if(i%2==0){
			$(this).removeClass('rowOdd');
			$(this).addClass('rowEven');
		}else{
			$(this).removeClass('rowEven');
			$(this).addClass('rowOdd');
		}
		$(this).mouseover(function(){
			$(this).css("background","#eee");
		});
		$(this).mouseout(function(){
			$(this).css("background","");
		});
	});
}

function listDefaultPageAjax(id,form,action){


	var submitButton=$.find("input[ajaxSearchBtn='true']")[0];
	$(submitButton).bind('click',{'gotopage':1},searchDataByPage);//jay:change gotopage:33 to gotopage:1


	$(document).ready(function(){
		//wan e zhi yuan
		$('#searchFormIdValue').remove();
		$('#searchFormActionValue').remove();
		$('#displayDataIdValue').remove();

		$('body').append($("<input style='display: none;' type='hidden' value='"+form+"' id='searchFormIdValue'/>"));
		$('body').append($("<input style='display: none;' type='hidden' value='"+action+"' id='searchFormActionValue'/>"));
		$('body').append($("<input style='display: none;' type='hidden' value='"+id+"' id='displayDataIdValue'/>"));
		searchDataByPage(1);
	});
}

function searchDataByPage(objParam){


	var param="";
	var submitFormId=$('#searchFormIdValue').val();
	var showDataId=$('#displayDataIdValue').val();
	var formAction=$('#searchFormActionValue').val();

	if(submitFormId){
		param+=$('#'+submitFormId).serialize();
	}

	var gotopage=1;
	if(objParam.hasOwnProperty("data")){
		gotopage=objParam.data.gotopage;
	}else{
		gotopage=objParam;
	}

	var pagesize=10;
	if($('#page_pagesize').val()){
		pagesize=$('#page_pagesize').val();
	}

	//param+='&gotopage='+gotopage+"&pagesize="+pagesize;
	param+='&page.page='+(gotopage)+"&page.size="+pagesize;//jay:change to spring data jpa default pageable parameter for more simple&common usage


	var loading=new ol.loading({id:showDataId});
	loading.show();

	if(param.charAt(0)=='&'){
		param=param.substring(1,param.length);
	}
	//param=decodeURIComponent(param,true);
	//alert("formAction:==>>  "+param);
//	$("#"+showDataId).load(formAction,param,function(){
//		loading.hide();
//		rowStyleClass();
//	});

	//param=decodeURIComponent(param);
	//alert(param);
	$.post(formAction, param, function(data) {
		$("#" + showDataId).html(data);
		loading.hide();
		rowStyleClass();
	});



}

function showRadioTree(url,id,setting,jsonHandler){
	if (!setting) {
		var setting = {
			check : {
				enable : true,
				chkStyle : 'radio',
				radioType : 'all'
			},
			view : {
				dblClickExpand : false
			},
			data : {
				simpleData : {
					enable : true
				}
			},
			callback : {
				onClick : function(e, treeId, treeNode) {
					var zTree = $.fn.zTree.getZTreeObj('treeNode');
					zTree.checkNode(treeNode, !treeNode.checked, null, true);
					return false;
				},
				onCheck : function onCheck(e, treeId, treeNode) {
					var zTree = $.fn.zTree.getZTreeObj('treeNode'), nodes = zTree.getCheckedNodes(true), v = '';
					for (var i = 0, l = nodes.length; i < l; i++) {
						v += nodes[i].name + ',';
					}
					if (v.length > 0)
						v = v.substring(0, v.length - 1);
					var cityObj = $('#'+id);
					cityObj.attr('value', v);
				}
			}
		};
	}
	buildSelectTree(setting,id,url,jsonHandler);
}


function buildSelectTree(setting,id,url,jsonHandler){
	if(!document.getElementById('treeNode')){
		$('body').append($("<div id='menuContent' class='menuContent' style='display:none; position: absolute;'><ul id='treeNode' class='ztree' style='margin-top:0; width:180px; height: 300px;'></ul></div>"));
	}

	if($("#"+id).val()==''){
		$("#"+id).val('Loading...');
	}

	$.getJSON(url, function(jsonData){
		try{
			if(jsonHandler!=null){
				jsonData=jsonHandler(jsonData);
			}
		}catch(e){
		}

		$.fn.zTree.init($("#treeNode"), setting, jsonData);
		var displayObj = $("#"+id);
		var displayOffset = $("#"+id).offset();

		if($("#"+id).val()=='Loading...'){
			$("#"+id).val('');
		}

		$("#menuContent").css({left:displayOffset.left + "px", top:displayOffset.top + displayObj.outerHeight() + "px"})
						 .slideDown("fast");

		$("body").bind("mousedown",{displayId:id},onTreeMenuBodyDown);
	});
}

function hideMenu() {
	$("#menuContent").fadeOut("fast");
	$("body").unbind("mousedown", onTreeMenuBodyDown);
}

function onTreeMenuBodyDown(event) {
	if (!(event.target.id == "menuBtn" || event.target.id == event.data.displayId || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length > 0)) {
		hideMenu();
	}
}






function popUpTree(obj,setting,zNodes){

	if(!document.getElementById('treeNode')){
		$('body').append($("<div id='menuContent' class='menuContent' style='display:none; position: absolute;'><ul id='treeNode' class='ztree' style='margin-top:0; width:180px; height: 300px;'></ul></div>"));
	}

	$.fn.zTree.init($("#treeNode"), setting, zNodes);
	var displayObj = $(obj);
	var displayOffset = $(obj).offset();


	$("#menuContent").css({left:displayOffset.left + "px", top:displayOffset.top + displayObj.outerHeight() + "px"}).slideDown("fast");

	$("body").bind("mousedown",{displayId:obj.getAttribute("id")},onTreeMenuBodyDown);

}









function debug(obj){
	var s="";
	for(var i in obj){
		s+=i+":"+obj[i]+"\n";
	}
	return s;
}

function debugArray(array){
	var s="";
	for(var i=0;i<array.length;i++){
		s+=array[i]+",";
	}
	return (s);
}

function addFunnScreemButton(){
	var buttons="<div onclick='javascript:openCollapseTop(this);' class='globalText' id='TopButton' style='color: blue;font-size: medium;position: absolute;top:30px;cursor: pointer;left: "+((window.screen.availWidth)/2+80)+"px;'>隐藏</div>"+
				"<div onclick='javascript:openCollapseLeft(this);' class='globalText' id='leftButton' style='color: blue;font-size: medium;position: absolute;left:20px;top:"+(window.screen.availHeight)/2+"px;cursor: pointer;'>隐藏</div>";
	$('body').append($(buttons));
}

function openCollapseTop(obj) {
	var topFrame = $(parent.parent.document.body);
	if (topFrame.attr("status") == 'open') {
		topFrame.attr("rows", "0,*");
		topFrame.attr("status", "close");
		obj.innerHTML="展开";
	} else {
		topFrame.attr("rows", "112,*");
		topFrame.attr("status", "open");
		obj.innerHTML="隐藏";
	}
}

function openCollapseLeft(obj) {
	var c = $(parent.document).children()[0];
	var frameset = $(c).children()[1];
	var leftFrameSet = $(frameset);
	if (leftFrameSet.attr("status") == 'open') {
		leftFrameSet.attr("cols", "1,*");
		leftFrameSet.attr("status", "close");
		obj.innerHTML="展开";
	} else {
		leftFrameSet.attr("cols", "220,*");
		leftFrameSet.attr("status", "open");
		obj.innerHTML="隐藏";
	}
}
